<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'north',border:false">
		<form class="appForm">
			<div class="datagrid-toolbar">
				<table class="appTable">
					<tr>
						<td>
							<input name="Q_key_=" data-options="prompt:'字典KEY'" class="easyui-textbox" type="text" style="width:100px;"/>
							<input name="Q_name_=" data-options="prompt:'字典分类名'" class="easyui-textbox" type="text" style="width:100px;"/>
							<button type="submit" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</button>
							<button type="button" onclick="$(this).parents('form').eq(0).form('reset');" class="easyui-linkbutton" data-options="iconCls:'icon icon-reset'">清空</button>
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div data-options="region:'center',border:false">
		<table class="appGrid" isTree="true"></table>
	</div>
</div>
<script type="text/javascript">
	var grids = $("table.appGrid");
	grids.eq(grids.size()-1).treegrid({
		url:_ctx+'/admin/dic/list',
		pagination:true,
		selectChildOnParent:false,
		columns:[[
			{field:'id',checkbox:true},
			{field:'key',title:'字典KEY',width:50},
			{field:'name',title:'字典名称',width:100},
			{field:'value',title:'字典值',width:100},
			{field:'orders',title:'排序',width:80,align:'center'},
			{field:'status',title:'有效',width:80,
				formatter: function(val){
					return $.appDicFormat('status',val);
				}
			}
		]],
		appToolbar: [{
			iconCls: 'icon icon-add',
			"key":'dic_addKind',
			text:'添加/编辑分类',
			handler: function(){
				var grid = getDatagrid(this);
				var rows = grid.datagrid("getSelections");
				var record = rows[0];
				if(rows.length!=1||!rows[0].key){
					record = null;
				}
				$("<div/>").dialog({
					title: '添加/编辑分类',
					href: _tpl+'/admin/dic_type_add.tpl.php',
					width:500,
					data:record,
					allowClose:record?true:false,
					grid:grid.get(0),
					buttons:[
						{
							text : '保存',
							iconCls : 'icon-save',
							handler : function() {
								var form = $(this).parents("div.window").eq(0).find("form").eq(0);
								form.submit();
							}
						}
					]
				});
			}
		},{
			iconCls: 'icon icon-add',
			text:'添加/编辑字典',
			"key":'dic_addItem',
			handler: function(){
				var grid = getDatagrid(this);
				var rows = grid.treegrid("getSelections");
				if(rows.length!=1){
					$.appAlert('请选择一个分类或字典项，进行操作！');
					return;
				}
				var record = rows[0];
				if(record.key){
					record = {
						pname:record.name,
						kind_id:record.id
					}
				}else{
					var parent = grid.treegrid('getParent',record.id);
					record.pname = parent.name;
					record.kind_id = parent.id;
				}
				$("<div/>").dialog({
					title: '添加/编辑字典',
					href: _tpl+'/admin/dic_add.tpl.php',
					width:500,
					data:record,
					allowClose:record.id?true:false,
					//grid:grid.get(0),
					buttons:[
						{
							text : '保存',
							iconCls : 'icon-save',
							handler : function() {
								var form = $(this).parents("div.window").eq(0).find("form").eq(0);
								form.submit();
							}
						}
					]
				});
			}
		},{
			iconCls: 'icon icon-delete',
			text:'删除',
			"key":'dic_del',
			handler: function(){
				var grid = getDatagrid(this);
				var rows = grid.treegrid("getSelections");
				if(rows.length==0){
					$.appAlert("请选择要删除的记录！");
					return;
				}
				var ids = [];
				$.each(rows,function(i,r){
					ids.push(r.id);
				});
				$.appAjax({
					url:_ctx+"/admin/dic/delete",
					data:{"ids":ids.join(',')},
					grid:grid.get(0),
					confirmTitle:'确认删除选择的记录？'
				});
			}	
		}]
	});
</script>